/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import negocio.*;
import negocio.dao.DAO;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author davidcabrera
 */
public class horarioAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "mostrarhorarios";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        DAO d = new DAO();
        List h = d.list("Horariopista");
        List<Horariopista> horarios = new ArrayList<Horariopista>();
        Integer id = Integer.valueOf(request.getParameter("id"));
        for (Object o : h) {
            Horariopista horario = (Horariopista) o;
            if (horario.getId().getIdPista() == id) {
                horarios.add(horario);
            }
        }
        List hor = d.list("Horario");
        List<Horario> horariofinal = new ArrayList<Horario>();
        List ocupado=new ArrayList();
        for (Horariopista horario : horarios) {
            int i = 0;
            boolean enc = false;
            while (i < hor.size() && enc == false) {
                Horario horario1 = (Horario) hor.get(i);
                if (horario.getId().getIdHorario() == horario1.getIdHorario()) {
                    horariofinal.add(horario1);
                    ocupado.add(horario.getOcupada());
                    enc = true;
                } else {
                    i++;
                }

            }

        }
        Pista pista=(Pista)d.read("Pista", id);
        
        request.setAttribute("pista", pista);
        request.setAttribute("ocupado", ocupado);
        request.setAttribute("horarios", horariofinal);
        
        
      
        return mapping.findForward(SUCCESS);
    }
}
